package cn.group7.padserver.service.impl;

import cn.group7.padserver.dao.UserDao;
import cn.group7.padserver.entity.Page;
import cn.group7.padserver.entity.RegisterUser;
import cn.group7.padserver.pojo.Expert;
import cn.group7.padserver.pojo.Knowledge;
import cn.group7.padserver.pojo.User;
import cn.group7.padserver.service.ExpertService;
import cn.group7.padserver.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 谷汉斌
 * @description 用户服务实现类
 * @createTime 2024/7/22 下午8:37
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private ExpertService expertService;

    /**
     * @param userName 用户名
     * @return 用户
     * @description 根据用户名查询用户
     */
    @Override
    public User selectByUserName(String userName) {
        return userDao.selectByPrimaryKey(userName);
    }

    /**
     * 查询所有用户
     *
     * @return 用户列表
     */
    @Override
    public List<User> selectAll() {
        return userDao.selectAll();
    }

    /**
     * 添加用户
     *
     * @param user 用户
     * @return 是否添加成功
     */
    @Override
    public boolean add(User user) {
        String encodePassword = new BCryptPasswordEncoder().encode(user.getPassword());
        user.setPassword(encodePassword);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        return userDao.insertSelective(user) == 1;
    }

    /**
     * 注册用户
     *
     * @param registerUser 注册用户
     * @return 是否注册成功
     */
    @Override
    public boolean register(RegisterUser registerUser) {
        //判断用户名是否重复
        if (null != userDao.selectByPrimaryKey(registerUser.getUsername())) {
            log.info("注册失败用户名{}重复了", registerUser.getUsername());
            return false;
        }
        //构建数据库实体类
        User user = User.builder()
                .userName(registerUser.getUsername())
                //密码加密
                .password(new BCryptPasswordEncoder().encode(registerUser.getPassword()))
                .nickName(registerUser.getNickname())
                .role("user")
                .createTime(new Date(System.currentTimeMillis()))
                .updateTime(new Date(System.currentTimeMillis()))
                .build();
        return userDao.insertSelective(user) == 1;
    }

    /**
     * 更新用户
     *
     * @param user 用户
     * @return 是否更新成功
     */
    @Override
    public boolean update(User user) {
        user.setUpdateTime(new Date());
        if(user.getRole().equals("expert")){
            Expert expert = new Expert();
            expert.setUserName(user.getUserName());
            expert.setPosition("xxx绝密");
            expert.setProfession("xxx绝密");
            expert.setRealName(user.getRealName()+user.getUserName());
            expert.setBelong("xxx绝密");
            expert.setPhone(user.getPhone());
            expertService.insert(expert);
        }
        return userDao.updateByPrimaryKeySelective(user)==1;
    }

    /**
     * 删除用户
     *
     * @param userName 用户名
     * @return 是否删除成功
     */
    @Override
    public boolean delete(String userName) {
        return userDao.deleteByPrimaryKey(userName)==1;
    }

    /**
     * 用户登录时更新用户信息
     *
     * @param user 用户
     * @return 是否更新成功
     */
    @Override
    public boolean loginUpdateByUsername(User user) {
        user.setUpdateTime(new Date());
        return userDao.updateByPrimaryKeySelective(user)==1;
    }

    /**
     * 分页查询用户
     *
     * @param pageNum  页码
     * @param pageSize 每页显示的条数
     * @return 分页信息
     */
    @Override
    public Page<User> findPage(Integer pageNum, Integer pageSize) {
        // 页码小于等于1，则默认为1
        if (pageNum <= 1) {
            pageNum = 1;
        }
        //开始页码-1*每页显示的条数
        int start = (pageNum - 1) * pageSize;
        Map<String, Object> map = new HashMap<>(1);
        map.put("start", start);
        map.put("pageSize", pageSize);
        Page<User> page = new Page<>();
        page.setData(userDao.queryUserByPage(map));
        page.setPageNum(pageNum);
        page.setTotal(userDao.selectTotal(map));
        page.setPageSize(pageSize);
        return page;
    }

    /**
     * 根据关键字分页查询用户
     *
     * @param keys     关键字
     * @param pageNum  页码
     * @param pageSize 每页显示的条数
     * @return 分页信息
     */
    @Override
    public Page<Knowledge> findPageByKeys(String keys, Integer pageNum, Integer pageSize) {
        // 页码小于等于1，则默认为1
        if (pageNum <= 1) {
            pageNum = 1;
        }
        //开始页码-1*每页显示的条数
        int start = (pageNum - 1) * pageSize;
        Map<String, Object> map = new HashMap<>(1);
        map.put("keys", keys);
        map.put("start", start);
        map.put("pageSize", pageSize);
        Page<Knowledge> page = new Page<>();
        page.setData(userDao.selectByKeys(map));
        page.setPageNum(pageNum);
        page.setTotal(userDao.selectTotal(map));
        page.setPageSize(pageSize);
        return page;
    }
}
